package leetcode_201_300;

import node.ListNode;

import java.util.ArrayList;
import java.util.List;

public class LeeCode_203 {
    public static void main(String[] args) {
        removeElements(new ListNode(1,new ListNode(2,new ListNode(6,
                new ListNode(3,new ListNode(4,new ListNode(5,new ListNode(6))))))), 6);
        removeElements(new ListNode(1,new ListNode(2,new ListNode(2,new ListNode(1)))), 2);
    }
    private static ListNode removeElements(ListNode head, int val) {
        if (head == null)
            return null;
        List<ListNode> list = new ArrayList<>();
        while (head != null){
            list.add(head);
            head = head.next;
        }
        head = list.get(0);
        int pre = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == head && list.get(i).val == val){
                head = head.next;
                pre = i;
                continue;
            }
            if (list.get(i).val == val){
                list.get(pre).next = list.get(i).next;
            }else {
                pre = i;
            }
        }
        return head;
    }
}
